import {Component, Vue} from 'vue-property-decorator'
import { errorMessage, isEmpty, successMessage } from '@/utils'
import * as http from '@/http/cms'


@Component
export default class Uploader extends Vue {

  // 上传
  public uploader: any = {
    imageType: '.jpg, .png, .jpeg',
    fileType: '.doc, .docx, .xls, .xlsx, .zip, .rar',
    action: '',
    progress: 0,
    finish: true,
    imageLoading: false,
    fileLoading: false
  }

  /**
   * 上传图片
   *
   * @param {String} uploadDir
   * @param {Object} params
   * @param {Object} [appendData]
   * @return {Promise<any>}
   */
  public uploadImage(uploadDir: string, params: any, appendData?: any): Promise<any> {
    const that = this,
      file = params.file,
      fileType = file.type,
      isImage = fileType.indexOf('image') !== -1

    if (!isImage) {
      errorMessage('仅支持图片文件，支持格式：' + this.uploader.imageType)
      return Promise.reject(false)
    }

    that.uploader.progress = 0
    that.uploader.finish = false
    return that.handleUploadImage(uploadDir, file, appendData).then((res) => {
      that.uploader.finish = true
      return res
    }).catch((err) => {
      that.uploader.finish = true
      errorMessage(err.message ? err.message : '发生未知错误，请联系管理员')
      return err
    })
  }

  /**
   * 处理上传图片
   *
   * @param {String} uploadDir 上传目录
   * @param {Object} file 上传文件
   * @param {Object} [appendData] 追加数据
   * @return
   */
  private handleUploadImage(uploadDir: string, file: any, appendData?: any): Promise<any> {
    // const formData = new FormData()
    // formData.append('upload_file', file)
    //
    // // 追加数据
    // if (!isEmpty(appendData)) {
    //   Object.keys(appendData).forEach((key) => {
    //     formData.append(key, appendData[key])
    //   })
    // }


    // 上传
    return http.uploadImage(this.setFormData(file, appendData), {
      onUploadProgress: (event: any) => {
        this.uploader.progress = parseInt((event.loaded / event.total * 100).toFixed(0))
      }
    })
  }

  /**
   * 上传文件
   *
   * @param {Object} params
   * @param {Object} [appendData]
   * @return {Promise<any>}
   */
  public uploadFile(params: any, appendData?: any): Promise<any> {
    const that = this,
      file = params.file,
      fileType = file.type

    // if (!isImage) {
    //   errorMessage('仅支持图片文件，支持格式：' + this.uploader.imageType)
    //   return Promise.reject(false)
    // }

    that.uploader.progress = 0
    that.uploader.finish = false
    return that.handleUploadFile(file, appendData).then((res) => {
      that.uploader.finish = true
      return res
    }).catch((err) => {
      that.uploader.finish = true
      errorMessage(err.message ? err.message : '发生未知错误，请联系管理员')
      return err
    })
  }

  /**
   * 处理上传文件
   *
   * @param {Object} file 上传文件
   * @param {Object} [appendData] 追加数据
   * @return
   */
  private handleUploadFile(file: any, appendData?: any): Promise<any> {
    return http.uploadFile(this.setFormData(file, appendData), {
      onUploadProgress: (event: any) => {
        this.uploader.progress = parseInt((event.loaded / event.total * 100).toFixed(0))
      }
    })
  }

  /**
   * 设置 FormData
   *
   * @param {Object} file
   * @param {Object} [appendData]
   * @protected
   */
  private setFormData(file: any, appendData?: any): FormData {
    const formData = new FormData()
    formData.append('upload_file', file)

    // 追加数据
    if (!isEmpty(appendData)) {
      Object.keys(appendData).forEach((key) => {
        formData.append(key, appendData[key])
      })
    }

    return formData
  }
}
